<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $searchModel backend\models\ActivitySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '活动列表';
$this->params['breadcrumbs'][] = $this->title;
$this->registerJs("

    $('#selection_all').click(function(){

        $('.i-checks').click();

    });

 

    $('#delete_select').click(function(){

        var selection = $('#grid').yiiGridView('getSelectedRows');

        if(selection.length==0){

            alert('未选中');

            return false;

        }else{

            if(confirm('您确定要删除吗?')==true){

               $.post('".Url::to(['activity/delete-select'])."', {selection:selection});

               location=location;

            }else{

               location=location;

            }        

        }

});

 

    $('#up_select').click(function(){

        var selection = $('#grid').yiiGridView('getSelectedRows');

        if(selection.length==0){

            alert('未选中');

            return false;

        }else{

            $.post('".Url::to(['activity/shelf'])."', {id:selection,status:1});

            location=location;

        }

    });

 

    $('#down_select').click(function(){

        var selection = $('#grid').yiiGridView('getSelectedRows');

        if(selection.length==0){

            alert('未选中');

            return false;

        }else{
            
            $.post('".Url::to(['activity/shelf'])."', {id:selection,status:0});

            location=location;

        }

    });

", \yii\web\View::POS_END);
?>
<div class="activity-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php Pjax::begin(); ?>
    <?php  echo $this->render('_search', ['model' => $searchModel]); ?>


    <?= GridView::widget([
        'dataProvider' => $dataProvider,
//        'filterModel' => $searchModel,
        'layout' => "{items}<div class='row'>

               <div class='btn-group col-md-3'>

                 <button class='btn btn-warning' id='selection_all'>全选/反选</button>

                 <button class='btn btn-default' id='up_select'>上架</button>

                 <button class='btn btn-default' id='down_select'>下架</button>

                 <button class='btn btn-default' id='delete_select'>删除</button>

               </div>

               <div class='col-md-7' style='margin-top:-20px'>

               {pager}</div>

                            </div>

               ",
        'options'=>['class'=>'grid-view','style'=>'overflow:hidden','id'=>'grid'],
        'columns' => [
            ['class' => 'yii\grid\CheckboxColumn' ,
                'checkboxOptions' => function($searchModel, $key, $index, $column) {
                    return ['value' => $searchModel->id,'class'=>'i-checks'];
                },
                'name'=>'ids',

            ],

            'id',
            'name',
            ['attribute'=>'activity_catalog','value'=>function($model){
                if($model->activity_catalog==0){
                    return '团购';
                }
            }],
            ['attribute'=>'start_time','value'=>function($model){
                return $model->start_time.'--'.$model->end_time;
            },'label'=>'活动时间'],
//            'banner',
//            'activity_catalog',
//            'peoples',
            //'start_user_type',
            //'join_user_type',
//            'start_time:datetime',
//            'end_time:datetime',
            //'goods:ntext',
            //'description:ntext',
            ['attribute'=>'status','format'=>'raw','value'=>function($model){
                return $model->status==1?'<span style="color:forestgreen">上架</span>':'<span style="color:darkred">下架</span>';
            }],

            ['class' => 'yii\grid\ActionColumn',
                'header'=>'操作',
                'template'=>'{shelf} {view} {update} {delete}',

                'buttons'=>[
                        'shelf'=>function($url,$model,$key){
                                return $model->status==1?Html::a(
                                        '下架',
                                        $url,
                                        [       'aria-label' => '下架',
                                                'data'=>[
                                                        'confirm' => '您确定要下架此活动吗？下架此活动前台将不在显示',
                                                        'method' => 'post',
                                                        'pjax' => '1',
                                                        'params'=>['id'=>$model->id]


                                                ]
                                        ]):Html::a('上架',$url,['data'=>[
                                                'method'=>'post',
                                                    'params'=>['id'=>$model->id]
                                ]]);
                            },
                        'view'=>function($url,$model){

                                return Html::a('查看', $url, [
                                    'title' => '查看',
                                    'aria-label' => '查看',
                                    'data-pjax' => '0',
                                ]);
                        },
                        'update'=>function($url,$model){

                            return $model->status==1?'':Html::a('修改', $url, [
                                'title' => '修改',
                                'aria-label' => '修改',
                                'data-pjax' => '0',
                            ]);
                        },
                        'delete'=>function($url,$model){

                            return $model->status==1?'':Html::a('删除', $url, [
                                'title' => '删除',
                                'aria-label' => '删除',
                                'data-pjax' => '0',
                                'data'=>[
                                    'confirm' => '您确定要删除此活动吗？删除后列表中将不在显示此活动',
                                    'method' => 'post'
                                ]
                            ]);
                        }
                        ],
                'urlCreator'=>function($action, $model){
                    switch($action)
                    {
                        case 'shelf':

                            return \yii\helpers\Url::toRoute(['activity/shelf']);//'javascript:shelfToggle('.$model->id.')';


                            break;

                        case 'view':
                            return ['view','id'=>$model->id];
                            break;
                        case 'update':
                            return ['update','id'=>$model->id];
                            break;
                        case 'delete':
                            return ['delete','id'=>$model->id];
                            break;

                    }

                }
                ],
        ],
    ]);

    ?>

    <?php Pjax::end(); ?>
</div>
